package job;

import java.io.*;

public class RenameFile {
    public static void main(String[] args) {
//        String path = "D:\\table\\兼职\\G2内容\\1 The Little Red Hen\\偶数";
//        String path = "D:\\table\\java\\Test_FileHandle\\新建文件夹";
//        renameEven(path);//偶数
//        renameOdd(path);//奇数

        //获取resources下的文件
        try {
            // 获取ClassLoader
            ClassLoader classLoader = RenameFile.class.getClassLoader();

            // 读取resources下的文件
//            InputStream inputStream = classLoader.getResourceAsStream("path_even.txt");//偶数
//            InputStream inputStream = classLoader.getResourceAsStream("path_odd.txt");//奇数
//            InputStream inputStream = classLoader.getResourceAsStream("test_renameFile.txt");
//            InputStream inputStream = classLoader.getResourceAsStream("singlePage.txt");//单页处理
//            InputStream inputStream = classLoader.getResourceAsStream("pairOfPages.txt");//双页处理_偶数
            InputStream inputStream = classLoader.getResourceAsStream("pairOfPages_1.txt");//双页处理_奇数

            if (inputStream == null) {
                throw new IllegalArgumentException("文件未找到!");
            } else {
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                    renameOdd(line);//奇数
//                    renameEven(line);//偶数
//                    rename(line);//单页处理
                }
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

//        String[] paths = new String[100];
//        for (int i = 0; i < 100; i++) {
//
//        }
    }

    //单页处理
    public static void rename(String path){
        File file = new File(path);
        if (file.exists()){
            File[] files = file.listFiles();
            int i = 1;
            for (File file1 : files) {
                System.out.println(file1.getName());
                String name = path+"\\"+i+".jpg";
                file1.renameTo(new File(name));
                if(i == 1)
                    i++;
                else
                    i+=2;
            }
            System.out.println("执行完成");
        }
    }

    //奇数
    public static void renameOdd(String path){
        File file = new File(path);
        File[] files = file.listFiles();
        int i = 1;
        for (File file1 : files) {
            System.out.println(file1.getName());
            String name = path+"\\"+i+".jpg";
            file1.renameTo(new File(name));
            i+=2;
        }
        System.out.println("执行完成");
    }
    //偶数
    public static void renameEven(String path){
        File file = new File(path);
        File[] files = file.listFiles();
        int i = 2;
        for (File file1 : files) {
            System.out.println(file1.getName());
            String name = path+"\\"+i+".jpg";
            file1.renameTo(new File(name));
            i+=2;
        }
        System.out.println("执行完成");
    }
}
